from typing import Optional, List
from datetime import datetime

from pydantic import BaseModel, Field


# 风险基础模型
class RiskBase(BaseModel):
    name: str
    description: Optional[str] = None
    status: str = "identified"
    probability: str = "medium"
    impact: str = "moderate"
    trigger_events: Optional[str] = None
    response_strategy: Optional[str] = None
    contingency_plan: Optional[str] = None


# 创建风险
class RiskCreate(RiskBase):
    project_id: int
    identified_date: datetime = Field(default_factory=datetime.utcnow)
    assignee_id: Optional[int] = None


# 更新风险
class RiskUpdate(BaseModel):
    name: Optional[str] = None
    description: Optional[str] = None
    status: Optional[str] = None
    probability: Optional[str] = None
    impact: Optional[str] = None
    risk_score: Optional[float] = None
    trigger_events: Optional[str] = None
    response_strategy: Optional[str] = None
    contingency_plan: Optional[str] = None
    updated_date: datetime = Field(default_factory=datetime.utcnow)
    closed_date: Optional[datetime] = None
    assignee_id: Optional[int] = None


# 风险评论
class RiskCommentCreate(BaseModel):
    risk_id: int
    content: str


# 问题基础模型
class IssueBase(BaseModel):
    name: str
    description: Optional[str] = None
    status: str = "open"
    priority: str = "medium"
    due_date: Optional[datetime] = None


# 创建问题
class IssueCreate(IssueBase):
    project_id: int
    reported_date: datetime = Field(default_factory=datetime.utcnow)
    assignee_id: Optional[int] = None
    related_risk_id: Optional[int] = None


# 更新问题
class IssueUpdate(BaseModel):
    name: Optional[str] = None
    description: Optional[str] = None
    status: Optional[str] = None
    priority: Optional[str] = None
    due_date: Optional[datetime] = None
    resolved_date: Optional[datetime] = None
    resolution: Optional[str] = None
    assignee_id: Optional[int] = None
    related_risk_id: Optional[int] = None


# 问题评论
class IssueCommentCreate(BaseModel):
    issue_id: int
    content: str


# API响应中的风险信息
class RiskResponse(RiskBase):
    id: int
    project_id: int
    creator_id: int
    assignee_id: Optional[int] = None
    risk_score: Optional[float] = None
    identified_date: datetime
    updated_date: Optional[datetime] = None
    closed_date: Optional[datetime] = None
    created_at: datetime
    updated_at: Optional[datetime] = None

    class Config:
        from_attributes = True


# API响应中的风险评论信息
class RiskCommentResponse(BaseModel):
    id: int
    risk_id: int
    user_id: int
    content: str
    created_at: datetime
    updated_at: Optional[datetime] = None

    class Config:
        from_attributes = True


# API响应中的问题信息
class IssueResponse(IssueBase):
    id: int
    project_id: int
    creator_id: int
    assignee_id: Optional[int] = None
    related_risk_id: Optional[int] = None
    reported_date: datetime
    resolved_date: Optional[datetime] = None
    resolution: Optional[str] = None
    created_at: datetime
    updated_at: Optional[datetime] = None

    class Config:
        from_attributes = True


# API响应中的问题评论信息
class IssueCommentResponse(BaseModel):
    id: int
    issue_id: int
    user_id: int
    content: str
    created_at: datetime
    updated_at: Optional[datetime] = None

    class Config:
        from_attributes = True